<?

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('id');


$sql = "SELECT * FROM `order` WHERE `id` = '$id'";
    $order_info = $db->get_one($sql);

	$tmp_ary = explode("{}", $order_info['product_info']);
	$info_ary = array();
	if (!empty($tmp_ary)) {
	    foreach ($tmp_ary as $v) {
	        @list($type, $pid, $count, $price, $name,$fitemid,$auction) = explode("|", $v);
	        $cost = $count*$price;
		$info_ary[$type . '_' . $pid.'_'.$fitemid] = array('name'=>$name,'fitemid'=>$fitemid, 'price'=>$price, 'count'=>$count, 'intype'=>$type, 'cost'=>$cost, 'auction'=>$auction);
	    }
	}
	
    
    $total_qty = $order_info['totalcount'];
    $total_cost = $order_info['totalcost'];
    
    $all_product_info_ary = array();
    foreach ($info_ary as $pid=>$vv) {
        @list($type, $pid) = explode("_", $pid);
    	$pid_ary[] = $pid;
    	
    }
    if (!empty($pid_ary)) {
    	$pid_str = implode(",", $pid_ary);
	$sql = "SELECT `id`, `code`, `small_pic`, `cbm`, `cuft` FROM `product` WHERE id IN (" . $pid_str . ")";
    	$query = $db->query($sql);
    	while($rs = $db->fetch_array($query)) {
    	    $all_product_info_ary[$rs['id']] = $rs;
    	}
    }
    
    $sql = "SELECT `id`, `username`, `firstname`, `lastname`, `city`, `countryid`, `type` FROM `user` WHERE `id` = '" . $order_info['user_id'] . "'";
	$user1_ary = $db->get_one($sql);
	//$money_type = $user1_ary['type'] == 1 ? "HKD" : "USD";	
	$money_type = "EUR";	

    $sql = "SELECT * FROM `inventory`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $location_info_ary[$rs['id']] = $rs['name'];
    }
	
    $order_time = date('F d, Y', $order_info['date']);
    $out_str = <<<EOT
    <span id="edit_area"><table width="700" border="0" cellspacing="1" cellpadding="1" align="left">
              <tr class="cartAlternatingRowB"  height="30">
              <td align="left" valign="top" class="cartHeading" colspan=6>
              
              <strong>Customer</strong>: {$user1_ary['firstname']}  
              <br><strong>City</strong>:  {$user1_ary['city']}
              <br><strong>Country</strong>: {$COUNTRY_ARY[$user1_ary['countryid']]}
              <br><strong>Order Date</strong>: {$order_time}
              <br><strong>Tracking No.</strong>: $id
              <br><br>
              </td>
              <td colspan=2 valign="top"><a href='jansenfurniture_order2.php?id={$id}'><img src="../images/admin/excel.gif" border=0><a></td>
              </tr>
              <tr><td colspan=9 bgcolor="#A5AAB0" height=1></td></tr>
              <tr class="cartAlternatingRowB"  height="30">
        						    <!--td align="center" valign="middle" class="cartHeading" width="40">View</td>
			    			        <td align="center" valign="middle" class="cartHeading" width="80">Sections</td-->
        			    			<td align="left" valign="middle" class="cartHeading" width="80"><strong>Item no.</strong></td>     
        			    			<td align="left" valign="middle" class="cartHeading" width="220" colspan=2><strong>Description</strong></td>        			    			
        			    			<td align="left" valign="middle" class="cartHeading" width="80"><strong>Finish</strong></td>    			    			
        			    			
        			    			<td align="right" valign="middle" class="cartHeading" width="60"><strong>Price<br>($money_type)</strong></td>
        			    			<td align="center" valign="middle" class="cartHeading" width="40"><strong>Qty</strong></td>
        			    			<td align="right" valign="middle" class="cartHeading" width="80"><strong>Total<br>($money_type)</strong></td>
        						</tr>
        						<tr><td colspan=9 bgcolor="#A5AAB0" height=1></td></tr>
EOT;

$total_cbm = $total_cuft = $total_qty = 0;

foreach ($info_ary as $k=>$v) {
    @list($type, $k, $fitemid) = explode("_", $k);
    $location = $v['intype'] == 0 ? 'Products' : $location_info_ary[$v['intype']];
    
    if (trim($all_product_info_ary[$k]['small_pic']) == "" || !file_exists("../attachments/" . $all_product_info_ary[$k]['small_pic'])) {
        $all_product_info_ary[$k]['small_pic'] = 'no_40.gif';
    }
    
    $all_product_info_ary[$k]['cbm'] = number_format($all_product_info_ary[$k]['cbm'], 2, '.', ',');
    $all_product_info_ary[$k]['cuft'] = number_format($all_product_info_ary[$k]['cuft'], 2, '.', ',');
    $v['price'] = number_format($v['price'], 2, '.', ',');
    $v['cost'] = number_format($v['cost'], 2, '.', ',');
	
    $total_cbm += $all_product_info_ary[$k]['cbm'];
    $total_cuft += $all_product_info_ary[$k]['cuft'];
    $total_qty += $v['count'];

	if($v['auction'] == 1)
	{
		$str_price = '<font color="red">'.$v['price']."</font>";
	}
	else 
	{
		$str_price = $v['price'];	
	}
    
$out_str .= <<<EOT

        			            <tr class="cartAlternatingRowB1" height=45>
        			                <!--td align="center" valign="middle" width="45"><img src="{$web_url}attachments/{$all_product_info_ary[$k]['small_pic']}" border="0" class="picture" width="40" height="40"></td>
			    			        <td align="center" valign="middle" width="80">$location</td-->			    			
        			    			<td align="left" valign="middle">{$all_product_info_ary[$k]['code']}</td>
        			    			<td align="left" valign="middle" colspan=2>{$v['name']} </td>        			 
        			    			<td align="left" valign="middle">{$_finish_item[$v['fitemid']]}</td>        			    			
        			    			      			    			
        			    			<td align="right" valign="middle">{$str_price}</td>
        			    			<td align="center" valign="middle">{$v['count']}</td>
        			    			<td align="right" valign="middle">{$v['cost']}</td>
        						</tr>

EOT;
} 

$total_cbm = number_format($total_cbm, 2, '.', ',');
$total_cuft = number_format($total_cuft, 2, '.', ',');
$total_cost = number_format($total_cost, 2, '.', ',');

$out_str .= <<<EOT
        						<tr height=30> 
                				  <td colspan=9></td>
              					</tr>
								<tr><td colspan=9 bgcolor="#A5AAB0" height=1></td></tr>
        						<tr>
        						  <td colspan=4 height=25 align="right"><strong>Sub Total:  </strong>&nbsp;&nbsp;</td>
        						  
        						
        						  <td colspan=1 align="center"></td>
        						  <td colspan=1 align="center">$total_qty</td>
        						  <td colspan=1 align="right">$total_cost</td>
        						</tr>   
        						
        						
              
              <tr height=30> 
                <td colspan=2></td>
              </tr>
            </table>
            
          </span>
EOT;



?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Jansenfurniture_order_<?=$id?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="./css/jansenfurniture.css" rel="stylesheet" type="text/css">
</head>
<body>

<?=$out_str?>

</body>
</html>